#include <stdio.h>
#include <glib-object.h>

#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
#include <thrift/c_glib/transport/thrift_buffered_transport.h>
#include <thrift/c_glib/transport/thrift_socket.h>

#include "gen-c_glib/calculator.h"

int main (void)
{
    ThriftSocket *socket;
    ThriftTransport *transport;
    ThriftProtocol *protocol;
    CalculatorIf *client;
    GError *error = NULL;
    int exit_status = 0;

#if (!GLIB_CHECK_VERSION (2, 36, 0))
    g_type_init ();
#endif

    socket    = g_object_new (THRIFT_TYPE_SOCKET, "hostname",  "localhost", "port", 9090, NULL);
    transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport", socket, NULL);
    protocol  = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL);

    thrift_transport_open (transport, &error);


    client = g_object_new (TYPE_CALCULATOR_CLIENT, "input_protocol",  protocol, "output_protocol", protocol, NULL);
    
   
    if (!error && calculator_if_print_hello_world (client, &error)) {
        puts ("C:print hello world success\n");
    }

    if (error) {
        printf ("ERROR: %s\n", error->message);
        g_clear_error (&error);
        exit_status = 1;
    }

    thrift_transport_close (transport, NULL);

    g_object_unref (client);
    g_object_unref (protocol);
    g_object_unref (transport);
    g_object_unref (socket);

    return exit_status;
}

